package comm

import (
	"io"
	"log"
	"net"
	"sync"
)

func TCPConnCopy(logger *log.Logger, conn1, conn2 net.Conn) {
	var wg sync.WaitGroup
	// wait tow goroutines
	wg.Add(2)
	go func() {
		n, err := io.Copy(conn1, conn2)
		logger.Printf("Copy1: %s -> %s <%d> %v", conn1.RemoteAddr().String(), conn2.RemoteAddr().String(), n, err)
		conn1.Close()
		wg.Done()
	}()
	go func() {
		n, err := io.Copy(conn2, conn1)
		logger.Printf("Copy2: %s -> %s <%d> %v", conn2.RemoteAddr().String(), conn1.RemoteAddr().String(), n, err)
		conn2.Close()
		wg.Done()
	}()

	//blocking when the wg is locked
	wg.Wait()
}
